﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class UserInRoleDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_USER_IN_ROLE = "spGetListPagingUserInRole";
        public const string SP_GET_LIST_PAGING_USER_IN_ROLE_BY_ID = "spGetUserInRoleById";
        public const string SP_CREATE_OR_UPDATE_USER_IN_ROLE = "spCreateOrUpdateUserInRole";
        public const string SP_GET_LIST_USER_OF_ROLE = "spGetListUserOfRoleInRoleID";
        public const string SP_DELETE_USER_IN_ROLE = "spDeleteUserInRole";
        public const string Param_UserInRoleID = "@UserInRoleID";
        public const string Param_UserID = "@UserID";
        public const string Param_RoleID = "@RoleID";
        public const string Param_RoleName = "@RoleName";
        public const string Param_UserName = "@UserName";


        public UserInRoleDAO(String connectionString, System.Type type = null)
            : base(connectionString, type)
        {

        }

        internal DataPage<UserInRoleModel> GetDynamicListPaging(UserInRoleModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_USER_IN_ROLE);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_UserInRoleID, model.UserInRoleID));
                    command.Parameters.Add(CreateParameter(Param_UserID, model.UserID));
                    command.Parameters.Add(CreateParameter(Param_RoleID, model.RoleID));
                    command.Parameters.Add(new SqlParameter("@AppId", model.AppId));
                    command.Parameters.Add(CreateParameter(Param_UserName, model.UserName));
                }
                command.Parameters.Add(CreateParameter("@PageIndex", pageIndex));
                command.Parameters.Add(CreateParameter("@PageSize", pageSize));
                return GetDataPage<UserInRoleModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal UserInRoleModel GetById(int? ID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_USER_IN_ROLE_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_UserInRoleID, ID));

                return GetSingle<UserInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<UserInRoleModel> GetListUserInRoleByRoleID(int? roleID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_USER_OF_ROLE);
                command.Parameters.Add(new SqlParameter(Param_RoleID, roleID));

                return GetList<UserInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateUserInRole(UserInRoleModel model, int[] listID)
        {
            try
            {
                foreach (int id in listID)
                {
                    SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_USER_IN_ROLE);
                    command.Parameters.Add(new SqlParameter("@" + UserInRoleModel.FIELD_USER_IN_ROLE_ID, model.UserInRoleID));
                    command.Parameters.Add(new SqlParameter("@" + UserInRoleModel.FIELD_USER_ID, id));
                    command.Parameters.Add(new SqlParameter("@" + UserInRoleModel.FIELD_ROLE_ID, model.RoleID));
                    command.Parameters.Add(new SqlParameter("@" + UserInRoleModel.FIELD_DESCRIPTION, model.Descriptions));
                    if (ExecuteQuery(command) <= 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteUserInRole(int[] ID)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_USER_IN_ROLE);
                command.Parameters.Add(new SqlParameter("@UserInRoleList", GetTableId<int>(ID, "MenuInRoleID")));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
    }
}
